package top.lgd.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lgd.base.CacheKeys;
import top.lgd.base.LgdMenu;
import top.lgd.base.LgdResult;
import top.lgd.controller.LgdController;
import top.lgd.enums.ResultEnum;
import top.lgd.pojo.entity.system.User;
import top.lgd.pojo.req.LoginReq;
import top.lgd.service.system.UserService;
import top.lgd.utils.IdUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/15 9:53
 */
@RestController
@RequestMapping("/admin/systemUser")
public class UserAdminController extends LgdController {

    @Autowired
    private UserService userService;

    /**
     * 系统用户登录
     *
     * @param loginReq
     * @return
     */
    @PostMapping("/login")
    public LgdResult<String> login(@RequestBody LoginReq loginReq) {
        User user = userService.getOne(new QueryWrapper<User>()
                .eq("username", loginReq.getUsername())
                .eq("password", loginReq.getPassword()));
        if (null != user) {
            final String token = IdUtils.fastSimpleUUID();
            lgdCacheService.put(CacheKeys.CURRENT_USER + token, user);
            return LgdResult.success(token);
        } else {
            return LgdResult.fail();
        }
    }

    /**
     * 查询当前用户
     *
     * @return
     */
    @GetMapping("/currentUser")
    public LgdResult<User> currentUser() {
        User user = checkCurrentUser();
        return null != user ? LgdResult.success(user) : LgdResult.fail(ResultEnum.登录状态失效);
    }

    /**
     * 查询当前菜单
     *
     * @return
     */
    @GetMapping("/currentMenu")
    public List<LgdMenu> currentMenu() {
        return initLgdMenu();
    }

    List<LgdMenu> initLgdMenu() {
        List<LgdMenu> lgdMenuList = new ArrayList<>();
        LgdMenu home = new LgdMenu("工作台", "/workbench", "", "");
        lgdMenuList.add(home);

        // 学习管理
        LgdMenu study = new LgdMenu("学习管理", "/study", "", "");
        study.setRoutes(Arrays.asList(
                new LgdMenu("学习总览", "/study/overview", "", ""),
                new LgdMenu("目录管理", "/study/catalog", "", ""),
                new LgdMenu("内容管理", "/study/content", "", "")
        ));
        lgdMenuList.add(study);

        User user =  current();
        if (user.getUserType().equals("admin")) {
            // 个人管理
            LgdMenu personal = new LgdMenu("个人管理", "/person", "UserOutlined", "");
            personal.setRoutes(Arrays.asList(
                    new LgdMenu("个人总览", "/person/overview", "", ""),
                    new LgdMenu("账户管理", "/person/account", "", ""),
                    new LgdMenu("收入管理", "/person/salary", "", "")
            ));
            lgdMenuList.add(personal);
        }

        // 学习管理
        LgdMenu operation = new LgdMenu("运营管理", "/operation", "", "");
        operation.setRoutes(Arrays.asList(
                new LgdMenu("运营总览", "/operation/overview", "", ""),
                new LgdMenu("会员管理", "/operation/member", "", ""),
                new LgdMenu("预约管理", "/operation/appointment", "", "")
        ));
        lgdMenuList.add(operation);


//        // 系统管理中心
//        LgdMenu system = new LgdMenu("系统管理中心", "/system", "SettingOutlined", "");
//        system.setRoutes(Arrays.asList(
//                new LgdMenu("用户管理", "/system/user", "", "./system/user"),
//                new LgdMenu("栏目管理", "/system/catalog", "", "./system/catalog"),
//                new LgdMenu("文章管理", "/system/article", "", "./system/article"),
//                new LgdMenu("表格", "/ListTableList", "", "./TableList")
//        ));
//        lgdMenuList.add(system);
//        // 系统管理中心
//
//        // 数据管理中心
//        LgdMenu data = new LgdMenu("数据管理中心", "/data", "DatabaseOutlined", "");
//        data.setRoutes(Arrays.asList(
//                new LgdMenu("指数管理", "/data/index", "", "./data/index"),
//                new LgdMenu("股票基本数据", "/data/stockBase", "", "./data/stockBase"),
//                new LgdMenu("股票分日数据", "/data/stockDay", "", "./data/stockDay")
//        ));
//        lgdMenuList.add(data);
//        // 数据管理中心
//
//        // 个人管理中心
//        LgdMenu personal = new LgdMenu("个人管理中心", "/personal", "UserOutlined", "");
//        personal.setRoutes(Arrays.asList(
//                new LgdMenu("资产概览", "/personal/balance", "", "./personal/balance"),
//                new LgdMenu("账户管理", "/personal/account", "", "./personal/account"),
//                new LgdMenu("计划管理", "/personal/plan", "", "./personal/plan"),
//                new LgdMenu("收藏管理", "/personal/favorite", "", "./personal/favorite"),
//                new LgdMenu("股票计划", "/personal/stockPlan", "", "./personal/stockPlan"),
//                new LgdMenu("股票执行", "/personal/stockExec", "", "./personal/stockExec")
//        ));
//        lgdMenuList.add(personal);
//        // 个人管理中心

        return lgdMenuList;
    }
}